package com.qf.student.web;

import com.alibaba.fastjson.JSON;
import com.qf.student.entity.Dept;
import com.qf.student.entity.Emp;
import com.qf.student.entity.Page;
import com.qf.student.service.IDeptService;
import com.qf.student.service.IEmpService;
import com.qf.student.service.impl.DeptServiceImpl;
import com.qf.student.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/Emp")
public class EmpController extends HttpServlet {

    private IEmpService emptService = new EmpServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String action = req.getParameter("action");

        System.out.println("action:" + action);

        if ("add".equals(action)) {
            addEmp(req, resp);
        } else if ("update".equals(action)) {
            updateEmp(req, resp);
        } else if ("byId".equals(action)) {
            byId(req, resp);
        } else if ("del".equals(action)) {
            deleteEmp(req, resp);
        } else if ("list".equals(action)) {
            getList(req, resp);
        } else if ("byName".equals(action)) {
            byName(req, resp);
        } else if ("batchDel".equals(action)) {
            batchDel(req, resp);
        } else if ("page".equals(action)) {
            page(req, resp);
        }
    }

    private void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 1、获取分页的参数
        String pageNum = req.getParameter("pageNum");
        String size = req.getParameter("size");

        // 2.封装Page对象
        Page page = new Page();

        // 这里判断做的事情，前端传递了分页参数就使用前端的，没有传递我就使用默认的
        if (pageNum != null && !"".equals(pageNum)) {
            page.setPageNum(Integer.parseInt(pageNum)); // 如果前端传递了分页的参数，就覆盖掉默认
        }
        if (size != null && !"".equals(size)) {
            page.setSize(Integer.parseInt(size)); // 如果前端传递了分页的参数，就覆盖掉默认
        }

        // 3.调用业务层查询当前页的也数据
        emptService.page(page); // 用传递

        // 4.添加到域对象
        req.setAttribute("page", page);

        // 5.跳转到视图
        req.getRequestDispatcher("emp/empList.jsp").forward(req, resp);
    }

    private void batchDel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String jsonStr = req.getReader().readLine(); // 读取一行
        // 非空判断
        if (jsonStr != null && !"".equals(jsonStr)) {
            List<Integer> empnoList = JSON.parseArray(jsonStr, Integer.class);
            emptService.batchDel(empnoList);
        }
    }

    private void byName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("ename");
        Emp emp = emptService.getByName(name);
        System.out.println("emp:" + emp);//null
        String jsonString = JSON.toJSONString(emp);
        resp.getWriter().write(jsonString);
    }

    private void byId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("empno");
        Emp dept = emptService.getById(Integer.parseInt(id));
        // 2.放到作用域
        req.setAttribute("emp", dept);

        // 3.跳转页面
        req.getRequestDispatcher("emp/updateEmp.jsp").forward(req, resp);
    }

    private void getList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 1.获取数据
        List<Emp> list = emptService.list();

        // 2.放到作用域
        req.setAttribute("empList", list);

        // 3.跳转页面
        req.getRequestDispatcher("emp/empList.jsp").forward(req, resp);

    }

    private void deleteEmp(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String deptno = req.getParameter("empno");
        boolean remove = emptService.remove(Integer.parseInt(deptno));
        responceClient(remove, resp);
    }

    private void updateEmp(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 1.获取表单参数
        // 1.获取表单参数
        String ename = req.getParameter("ename");
        String job = req.getParameter("job");
        String sal = req.getParameter("sal");
        String comm = req.getParameter("comm");
        String deptno = req.getParameter("deptno");
        String mgr = req.getParameter("mgr");
        String hiredate = req.getParameter("hiredate");
        String empno = req.getParameter("empno");

        // 2.封装
        Emp emp = new Emp();
        emp.setEname(ename);
        emp.setEmpno(Integer.parseInt(empno));
        emp.setJob(job);
        emp.setMgr(Integer.parseInt(mgr));
        emp.setComm(Double.parseDouble(comm));
        emp.setSal(Double.parseDouble(sal));
        emp.setMgr(Integer.parseInt(mgr));
        emp.setDeptno(Integer.parseInt(deptno));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date2 = sdf.parse(hiredate);
            emp.setHiredate(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        // 3.插入，响应
        responceClient(emptService.update(emp), resp);
    }

    private void addEmp(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 1.获取表单参数
        String ename = req.getParameter("ename");
        String job = req.getParameter("job");
        String sal = req.getParameter("sal");
        String comm = req.getParameter("comm");
        String deptno = req.getParameter("deptno");
        String mgr = req.getParameter("mgr");
        String hiredate = req.getParameter("hiredate");

        // 2.封装
        Emp emp = new Emp();
        emp.setEname(ename);
        emp.setJob(job);
        emp.setMgr(Integer.parseInt(mgr));
        emp.setComm(Double.parseDouble(comm));
        emp.setSal(Double.parseDouble(sal));
        emp.setMgr(Integer.parseInt(mgr));
        emp.setDeptno(Integer.parseInt(deptno));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date2 = sdf.parse(hiredate);
            emp.setHiredate(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 3.插入
        boolean save = emptService.save(emp);

        // 4.响应
        responceClient(save, resp);
    }

    public void responceClient(boolean flag, HttpServletResponse resp) throws IOException {
        if (flag) {
            resp.sendRedirect("/Emp?action=list");
        } else {
            resp.getWriter().write("操作失败");
        }
    }
}
